﻿using System;
using System.Collections.Generic;
using App_Code.Rules;
using PluginDB;

namespace App_Code.SiteParsers
{
    /// <summary>
    /// Summary description for BaseScanner
    /// </summary>
    public class BaseScanner : ISiteScanner
    {
        #region Protected Members
        protected List<IRule> _moviesRules = new List<IRule>(); 
        #endregion

        #region Public Delegates
        public DlgAddVideoHandler _dlgAddVideoHandler;
        public DlgIsVideoExistsHandler _dlgIsVideoExistsHandler;
        #endregion

        #region Properties
        public string Name { get; set; }

        public DlgIsVideoExistsHandler DlgIsVideoExistsHandler
        {
            get
            {
                return _dlgIsVideoExistsHandler;
            }
            set
            {
                _dlgIsVideoExistsHandler = value;
            }
        }

        public DlgAddVideoHandler DlgAddVideoHandler
        {
            get
            {
                return _dlgAddVideoHandler;
            }
            set
            {
                _dlgAddVideoHandler = value;
            }
        }
        #endregion

        #region Ctor
        public BaseScanner()
        {
            this._moviesRules.Add(new IsHebDubMovieRule());
            this._moviesRules.Add(new IsHebSubMovieRule());
            this._moviesRules.Add(new ExtractSeriesNameRule());
            //this._moviesRules.Add(new VerifyBrokenLinkRule());
            this._moviesRules.Add(new ExtractMovieInfoRule());
        } 
        #endregion

        #region Public Methods
        public virtual int GetMoviesLastPageNumber()
        {
            return 0;
        }

        public virtual void ScanForMovies(int pagesToScan)
        {
            throw new NotImplementedException();
        }

        public virtual int GetSeriesLastPageNumber()
        {
            return 0;
        }

        public virtual void ScanForSeries(int pagesToScan)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region Protected Methods
        protected bool IsVideoExists(string pluginUrl)
        {
            if (this.DlgIsVideoExistsHandler == null) return false;

            return this.DlgIsVideoExistsHandler(pluginUrl);
        }

        protected void AddVideoToDB(PluginVideoInfo pluginVideoInfo)
        {
            if (this.DlgAddVideoHandler == null) return;

            this.ApplyVideoRules(pluginVideoInfo);

            this.DlgAddVideoHandler(pluginVideoInfo);
        }

        protected string CleanString(string str)
        {
            str = str.Replace("\r", string.Empty);
            str = str.Replace("\n", string.Empty);
            str = str.Replace("\t", string.Empty);
            return str;
        }

        protected void ApplyVideoRules(PluginVideoInfo pPluginVideoInfo)
        {
            foreach (var rule in _moviesRules)
            {
                rule.ApplyRule(pPluginVideoInfo);
            }
        }
        #endregion
    }
}